<?php
namespace app\shop\controller;

use think\Request;
use think\Exception;
use app\shop\model\ClientAddress as ClientAddressModel;
class Clientaddress extends \ShopController
{

    //新增
    public function save(Request $request)
    {
        $params = $request->post();
        $ClientAddressModel = new ClientAddressModel();
        $params['area'] = implode(',', $params['area']);
        $y  = $ClientAddressModel->where('client_id',$params['client_id'])->find();
        if(!$y){
            $params['is_default'] = 1;
        }
        try {
            $ClientAddressId = $ClientAddressModel->insertGetId($params);
            $data = $ClientAddressModel->where('client_address_id',$ClientAddressId)->find();
            $data['area'] = explode(',',$data['area']);
            $this->successTerminate($data);
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }
    //修改
    public function update(Request $request){
        $params = $request->post();
        $ClientAddressModel = new ClientAddressModel();
        $params['area'] = implode(',', $params['area']);

        try {
            $ClientAddressModel->where('client_address_id',$params['client_address_id'])->update($params);
            $this->successTerminate($ClientAddressModel);
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }
    //设为默认地址
    public function setDefault(Request $request){
        $params = $request->post();
        $ClientAddressModel = new ClientAddressModel();
        try {
            $ClientAddressModel->where('client_id',$params['client_id'])->update(['is_default'=>0]);
            $ClientAddressModel->where('client_address_id',$params['client_address_id'])->update(['is_default'=>1]);
            $this->successTerminate($ClientAddressModel);
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }

    }
    //获取客户默认地址
    public function getDefault(Request $request){
        try {
            $params = $request->get();
            $ClientAddressModel = new ClientAddressModel();
            $data = $ClientAddressModel->where('client_id',$params['client_id'])->where('is_default',1)->find();
            if(!empty($data)){
                $data['area'] = explode(',',$data['area']);
            }else{
                $data = null;
            }
            $this->successTerminate($data);

        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }
    //删除
    public function del(Request $request){
        $params = $request->get();
        $ClientAddressModel = new ClientAddressModel();
        try {
            $is_default = $ClientAddressModel->where('client_address_id',$params['client_address_id'])->find();
            if($is_default['is_default']){
                $this->serverErrorTerminate('已选中商品不允许删除');
            }
            $ClientAddressModel->where('client_address_id',$params['client_address_id'])->update(['is_delete'=>1]);

            $this->successTerminate($ClientAddressModel);
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }

    }
    //查询
    public function all(Request $request){
        $params = $request->get();
        $ClientAddressModel = new ClientAddressModel();
        try {
            $where['client_id'] = $params['client_id'];
            $where['is_delete'] = 0;
            if(!empty($params['client_address_id'])){
                $where['client_address_id'] =  $params['client_address_id'];
            }
           
            $data = $ClientAddressModel->where($where)->select();
            foreach($data as &$v){
                $v['area'] = explode(',',$v['area']);
            }
            $this->successTerminate($data);
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }
}